package advertising

import (
	"encoding/json"
	"gitee.com/alex_li/amazon-rpt/internal/rest"
	"github.com/lishimeng/go-log"
)

type TextFilter struct {
	Include []string `json:"include,omitempty"`
}

type KeywordUpdateItem struct {
	KeywordId string  `json:"keywordId,omitempty"`
	State     string  `json:"state,omitempty"`
	Bid       float64 `json:"bid,omitempty"`
}

// KeywordListReq
// StateFilter: Items Enum:
//                         "ENABLED" "PAUSED" "PROPOSED"
//                         "ARCHIVED" "ENABLING" "USER_DELETED" "OTHER"

type KeywordListReq struct {
	NextToken       string      `json:"nextToken,omitempty"`
	MaxResults      int         `json:"maxResults,omitempty"`
	StateFilter     *TextFilter `json:"stateFilter,omitempty"`
	KeywordIdFilter *TextFilter `json:"keywordIdFilter,omitempty"`
}

type KeywordInfoV3 struct {
	KeywordId   int64   `json:"keywordId,omitempty"`
	CampaignId  int64   `json:"campaignId,omitempty"`
	MatchType   string  `json:"matchType,omitempty"`
	State       string  `json:"state,omitempty"`
	Bid         float64 `json:"bid,omitempty"`
	AdGroupId   int64   `json:"adGroupId,omitempty"`
	KeywordText string  `json:"keywordText,omitempty"`
}

type KeywordInfo struct {
	KeywordId   string  `json:"keywordId,omitempty"`
	CampaignId  string  `json:"campaignId,omitempty"`
	MatchType   string  `json:"matchType,omitempty"`
	State       string  `json:"state,omitempty"`
	Bid         float64 `json:"bid,omitempty"`
	AdGroupId   string  `json:"adGroupId,omitempty"`
	KeywordText string  `json:"keywordText,omitempty"`
}

type KeywordListResp struct {
	Keywords []KeywordInfo `json:"keywords,omitempty"`
	AmazonPager
}

type KeywordUpdateReq struct {
	Keywords []KeywordUpdateItem `json:"keywords,omitempty"`
}

type KeywordOperationResponse struct {
	id string
}

type KeywordUpdateResItem struct {
}
type KeywordUpdateResp struct {
	Keywords []KeywordUpdateResItem `json:"keywords,omitempty"`
}

func (c *Client) UpdateSpKeyword(req KeywordUpdateReq) (resp map[string]any) {

	const action = "/sp/keywords"
	resp = make(map[string]any)
	r, err := c.request()
	if err != nil {
		return
	}
	bs, _ := json.Marshal(req)
	log.Info(string(bs))
	err = r.Path(action).Body(req).Response(&resp).
		Expect(207).
		Accept("application/vnd.spKeyword.v3+json").
		Json("PUT", "application/vnd.spKeyword.v3+json")
	if err != nil {
		log.Info(err)
		return
	}
	return
}

func (c *Client) Recommend(req any) {

	const action = "/sp/targets/keywords/recommendations"

	var resp any
	r, err := c.request()
	if err != nil {
		return
	}
	err = r.Path(action).Body(req).
		Expect(207).
		Response(&resp).
		EnableDebug(true).
		Json(rest.POST)
	if err != nil {
		return
	}
}

func (c *Client) ListSpKeyword(req KeywordListReq) (resp KeywordListResp, err error) {
	const action = "/sp/keywords/list"

	r, err := c.request()
	if err != nil {
		return
	}
	bs, _ := json.Marshal(req)
	log.Info(string(bs))
	err = r.Path(action).Body(req).Response(&resp).
		Accept("application/vnd.spKeyword.v3+json").
		Json(rest.POST, "application/vnd.spKeyword.v3+json")
	if err != nil {
		log.Info(err)
		return
	}
	return
}
